import store from '@/store/index'
import {themeInfo} from '@/utils/baseConfig'
import {hxHandler, onHxMessage} from "@/utils/hxHandler";
import {postMessage} from "@/utils/hxHandler";




export function initApp(data={}){

    let baseConfig = localStorage.getItem('baseConfig')
    baseConfig?baseConfig=JSON.parse(baseConfig):baseConfig={}

    setTimeout(()=>{
        console.log(baseConfig)
    },10000)
    // 初始化主题颜色
    if (!baseConfig.themeInfo)baseConfig.themeInfo=themeInfo
    store.commit('app/setThemeInfo', baseConfig.themeInfo)

    // 初始化AI配置
    if(baseConfig.apiKey)store.commit('app/setApiKey', baseConfig.apiKey)
    if(baseConfig.baseUrl)store.commit('app/setBaseUrl', baseConfig.baseUrl)
    if(baseConfig.setActiveModel)store.commit('app/setActiveModel', baseConfig.modelName)



    // 初始化Hx消息监听
    onHxMessage()

    // 向Hx发送初始化配置请求
    window.addEventListener("hbuilderxReady", ()=>{
        try {
            setTimeout(()=>{
                postMessage({
                    command:'initApp',
                    data:''
                })
            },1000)
        } catch (error) {
            console.log(error);
        }
    });



    let models = store.state.app.aliModels||[]
    data.models?store.commit('app/setActiveModel',data.models):store.commit('app/setActiveModel',models[0])
    let message = `##### 您好！我是您的开发助手ucoder，我可以：
- ✓ 解答Hbuilder环境配置问题
- ✓ 提供Vue/uni-app代码示例
- ✓ 分析运行时错误原因
- ✓ 推荐性能优化方案
##### 请直接描述您的问题或粘贴相关代码片段~`
    store.commit('app/setChatHistoryMessage',{ role: "assistant", content: message })
}